/*
Realistic Unit Modification v4.0 Dialog 
v4.0.0.1 [ALPHA]
Concept & Development by Draper & Raps | [VRC]
*/

private ["_arry","_heloname","_callsign","_home","_airport","_egress","_ingress","_helo","_class","_lz"];

/* sets the variables passed from the PDA selection */
_arry = _this;
_heloname = _arry select 0;
_callsign = _arry select 1;
_home = _arry select 2;
_airport = _arry select 3;
_egress = _arry select 4;
_ingress = _arry select 5;
_helo = _arry select 0;
_class = typeof _helo;

/* checks to make sure that the helo is not already tasked */
if (_helo getVariable "RUM_inFlight") exitwith {hint "helo in flight";};

/* sets the helo as busy and removes from PDA */
_helo setVariable ["RUM_inFlight", true, true];

// _paytar = [];
RUM_ASclick = false;

/* open map and make selections for CAS mission */
openMap [true,true];
for [{_n = 1},{_n <= 3},{_n = _n + 1}] do {
	_mkrname = "";
	_type = "";
	_hint = "";
	switch (_n) do {
		case 1: {
			_mkrname = format ["%1target",_helo];
			_type = "mil_destroy";
			_hint = "Select Target Area for CAS";
		};
		case 2: {
			_mkrname = format ["%1ingress",_helo];
			_type = "mil_start";
			_hint = "Select Ingress Point";
		};
		case 3: {
			_mkrname = format ["%1egress",_helo];
			_type = "mil_end";
			_hint = "Select Egress Point";
		};
		default {
			hint "oops!";
		};
	};
	_asloc = createMarkerLocal [_mkrname, [0,0,0]];
	_asloc setMarkerShapeLocal "ICON";
	_mkrname setMarkerTypeLocal _type;
	_stgtar = "";

	if (_n == 1) then {
		// _stgtar = format ["'%1' setMarkerPosLocal _pos,RUM_ASclick = [_pos] call ROEzone",_mkrname];
	_mapclick = ["rum_mapclick_id", "onMapSingleClick", { _this setMarkerPosLocal _pos;RUM_ASclick = [_pos] call ROEzone; }, _mkrname] call BIS_fnc_addStackedEventHandler;
	} else {
		// _stgtar = format ["'%1' setMarkerPosLocal _pos,RUM_ASclick = true",_mkrname];
	_mapclick = ["rum_mapclick_id", "onMapSingleClick", { _this setMarkerPosLocal _pos;RUM_ASclick = true; }, _mkrname] call BIS_fnc_addStackedEventHandler;
	};

	titleText [_hint, "PLAIN DOWN"];
	waitUntil {RUM_ASclick};

	if ((_n == 2) or (_n == 3)) then {
		_mkrname2 = format ["%1target",_helo];
		_mkrname setMarkerDirLocal ((getMarkerPos _mkrname2 select 0)-(getMarkerPos _mkrname select 0)) atan2 ((getMarkerPos _mkrname2 select 1)-(getMarkerPos _mkrname select 1));
	};
	_mapclick = ["rum_mapclick_id", "onMapSingleClick"] call BIS_fnc_removeStackedEventHandler;
	RUM_ASclick = false;
};

/* close map */
titleFadeOut 0;
sleep 1;
openMap [false,false];

_helo setVariable ["RUM_inFlight", true, true];
_arry set [0,_helo];

/* create units to fly helo - client side */
sleep 1.5;
_grp = createGroup side player;
_pilot = _grp createUnit [RUM_AHEL_Pilot, getpos _home, [], 0, "FORM"];
_pilot moveinDriver _helo;
_gunner = _grp createUnit [RUM_AHEL_Pilot, getpos _home, [], 0, "FORM"];
_gunner moveinGunner _helo;

/* deletes any additional gunner created if any so there are no extra units running around */
if (!(_gunner in crew _helo)) then {
	deleteVehicle _gunner;
};

/* forces the server side helo object to become client side. */
[_helo] joinSilent player;
[_helo] joinSilent grpNull;
[_helo] joinSilent _grp;
_helo setBehaviour "COMBAT";
_helo setCombatMode "RED";

/* dialog from pilot to player */
["immediatestrike",[player,_pilot,_callsign]] execVM (RUM_PATH + "AHel\radiodialogs.sqf");
sleep 16;

/* turns engines on */
_helo engineOn true;

/* creates the waypoints */
for [{_n = 1},{_n <= 5},{_n = _n + 1}] do {
	_mkrname = "";
	switch (_n) do {
		case 1: {
			[_mkrname,_grp,_helo,_egress] call RUM_AHel_moveWPTakeoff;
			[_mkrname,_grp,_helo,_home,_egress] call RUM_AHel_moveWPNormal;
		};
		case 2: {
			_mkrname = format ["%1ingress",_helo];
			[_mkrname,_grp] call RUM_AHel_movewaypoint;
		};
		case 3: {
			_mkrname = format ["%1target",_helo];
			[_mkrname,_grp] call RUM_AHel_SAD;
		};
		case 4: {
			_mkrname = format ["%1egress",_helo];
			[_mkrname,_grp] call RUM_AHel_movewaypoint;
		};
		case 5: {
			[_mkrname,_grp,_home,_helo,_ingress] call RUM_AHel_LandHome;
		};
		default {
			hint "oops!";
		};
	};
};

/* delete all the markers that were created for this instance. */
_mkrname1 = format ["%1ingress",_helo];
_mkrname2 = format ["%1target",_helo];
_mkrname3 = format ["%1egress",_helo];
deletemarkerLocal _mkrname1;
deletemarkerLocal _mkrname2;
deletemarkerLocal _mkrname3;

/* turn helos engines off, remove pilot and gunner */
hint "deleting pilots and stuff";
_pilot action ["engineOff", _helo];
unassignVehicle _pilot;
doGetOut [_pilot,_gunner];
unassignVehicle _gunner;
doGetOut [_pilot,_gunner];
deleteVehicle _pilot;
deleteVehicle _gunner;

/* removed any invisible helo pads that were created */
if (!isNil "_lz") then {
	deleteVehicle _lz;
};

/* waits till crew are deleted */
waitUntil{
	sleep 0.5;
	(count(crew _helo) == 0);
};

/* set the helo back in the correct position and direction */
if (alive _helo) then {
	player sidechat format ["%1 has landed safely.", _callsign];
	player sidechat format ["%1 is being rearmed/refueled at this time and will be avaiable in %2 mics.", _callsign, round(RUM_AHEL_ReSupTime/60)];
	_helo setdir getdir _home;

	/* repairs, rearms and refuels the helo */
	_helo setDamage 0;
	_helo setFuel 1;
	_mags = magazinesAmmo _helo;
	// _mags = getArray (configFile>>"cfgVehicles">>typeOf _helo>>"Turrets">>"MainTurret">>"magazines");
	{_helo removeMagazine _x} forEach magazines _helo;
	{_helo addMagazine _x} forEach _mags;
};

/* timer for when the helo is avaialble for use again */
RUM_AHelAircraft = RUM_AHelAircraft + [_arry];
publicVariable "RUM_AHelAircraft";

_helo setVariable ["RUM_inFlight", false, true];

/* exits script */
if (true) exitwith {};
